package com.kamloo.stg.fragment;

import java.util.ArrayList;

import com.kamloo.stg.adapter.TabBusLineArrayAdapter;
import com.kamloo.stg.data.db.Line;
import com.kamloo.stg.data.db.LineDAO;
import com.kamloo.stg.dialog.TabBusDirectionDialog;
import com.kamloo.stg.info.AppConstant;
import com.kamloo.stg.R;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TabBus extends Fragment 
{
	//----- GLOBAL VARIABLE -----	
	private ListView busLineList;
	ArrayList<String> m_values;;
	ArrayList<Line> m_lines;
	
	
	//----- LISTENER -----
	OnItemClickListener busLineClickListener = new OnItemClickListener()
    {
    	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    	{
    		FragmentManager manager = getActivity().getSupportFragmentManager();
    		TabBusDirectionDialog dialog = new TabBusDirectionDialog();
   		    
    		int lineId = Integer.parseInt((String) ((TextView)view.findViewById(R.id.tabBusLineRowLineId)).getText());
    		    		    		    		
    		dialog.initializeView(lineId);    		
    		dialog.show(manager, AppConstant.FRAGMENT_TAB_BUS_DIRECTION_DIALOG);
    	}
    };
       
    
    TextWatcher busSearchWatcher = new TextWatcher() 
    {
        public void onTextChanged(CharSequence s, int start, int before, int count) 
        {	  
        	ArrayList<String> values = new ArrayList<String>();
        	ArrayList<Line> lines = new ArrayList<Line>();
        	
        	filterData(s.toString().toLowerCase(), values, lines);
        	TabBusLineArrayAdapter newAdapter = new TabBusLineArrayAdapter(getActivity(), 
        																   values.toArray(new String[values.size()]), 
        																   lines);
        	busLineList.setAdapter(newAdapter);
        }

		public void afterTextChanged(Editable s) {}

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    };	  
	
	
    //----- CLASS CODE -----	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    	
    	if (container == null) {
    		return null;
    	}
    	 
    	return (LinearLayout)inflater.inflate(R.layout.tab_bus, container, false);    	
    }
    
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {    	
    	m_values = new ArrayList<String>();
    	m_lines = new ArrayList<Line>();
    	
    	busLineList = (ListView) view.findViewById(R.id.tabBus_busLineList);
    	EditText busSearch = (EditText) view.findViewById(R.id.tabBus_SearchBox);
    	
    	setBusLineData();
    	TabBusLineArrayAdapter adapter = new TabBusLineArrayAdapter(getActivity(), 
    																m_values.toArray(new String[m_values.size()]), 
    																m_lines);
    
    	// Assign adapter and listener to ListView
    	busLineList.setAdapter(adapter);
    	busLineList.setOnItemClickListener(busLineClickListener);
    	busSearch.addTextChangedListener(busSearchWatcher);    	
    }
    
    private void setBusLineData() {
    	LineDAO lineDao = new LineDAO(getActivity());
    	lineDao.open();
    	
    	ArrayList<Line> lines = lineDao.selectAll();
    	for (Line line : lines) {
    		m_lines.add(line);
    		m_values.add(line.getName());
    	}
    	
    	lineDao.close();
    }
    
    
    private void filterData(String filter, ArrayList<String> values, ArrayList<Line> lines)
    {
    	for (int i = 0; i < m_values.size(); i++)
    	{
    		if (m_lines.get(i).getName().toLowerCase().contains(filter))
    		{
    			lines.add(m_lines.get(i));
    			values.add(m_lines.get(i).getName());
    		}
    	}
    }
    
    @Override
    public void onDestroy() {    
    	super.onDestroy();
    	try{
    	InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);		
        IBinder binder = getActivity().getCurrentFocus().getWindowToken();
      	imm.hideSoftInputFromWindow(binder, 0);
    	} catch (Exception e) {/*Toast.makeText(getActivity(), "troll", Toast.LENGTH_LONG).show();*/}
    }
}
